var BXUrlPreview=function(e){this.element=e;this.inputElement=null;this.id=this.element.id;this.carouselElement=null;this.carouselImages=[];this.currentImageId=null;this.init()};BXUrlPreview.prototype.init=function(){this.inputElement=this.element.querySelector(".urlpreview__ufvalue");this.initCarousel();this.bindEventHandlers();if(BX.type.isDomNode(this.inputElement)&&this.inputElement.form){BX.addCustomEvent(this.inputElement.form,"onAutoSaveRestoreFinished",function(e,t){if(this.inputElement.form==e.FORM&&BX.type.isNotEmptyString(t[this.inputElement.name])){this.attachUrlPreview({id:t[this.inputElement.name]})}}.bind(this))}};BXUrlPreview.prototype.detachUrlPreview=function(){if(this.inputElement){this.inputElement.value=""}this.element.style.display="none"};BXUrlPreview.prototype.attachUrlPreview=function(e){var t,i;if(this.element.style.display!=="none"){return}i=this.element.getAttribute("data-field-id");requestParams={action:"attachUrlPreview",userFieldId:i,elementId:this.id,sessid:BX.bitrix_sessid()};if(e.hasOwnProperty("url"))requestParams.url=e.url;else if(e.hasOwnProperty("id"))requestParams.id=e.id;else return;BX.ajax({url:"/bitrix/components/bitrix/main.urlpreview/ajax.php",method:"POST",data:requestParams,onsuccess:function(e){if(e.length>0){var t=document.createElement("div");var i=this.element.style.cssText;t.innerHTML=e;var r=t.firstElementChild;this.element.parentNode.replaceChild(r,this.element);this.element=r;this.element.style.cssText=i;this.element.style.removeProperty("display");this.init()}}.bind(this)})};BXUrlPreview.prototype.bindEventHandlers=function(){var e=this;var t=this.element.querySelector(".urlpreview__detach");if(t){t.addEventListener("click",e.detachUrlPreview.bind(e))}var i=this.element.querySelector(".urlpreview__container-switchable");if(i){i.addEventListener("click",BXUrlPreview.showEmbed)}if(this.carouselElement){var r=this.carouselElement.querySelector(".urlpreview__button-prev");var n=this.carouselElement.querySelector(".urlpreview__button-next");if(r)r.addEventListener("click",e.previousImage.bind(e));if(n)n.addEventListener("click",e.nextImage.bind(e))}};BXUrlPreview.prototype.initCarousel=function(){var e;var t;var i;var r;if(e=this.element.querySelector(".urlpreview__carousel")){this.carouselElement=e;t=e.querySelectorAll(".urlpreview__image");for(i=0;i<t.length;i++){t[i].dataset.imageId=i;this.carouselImages[i]=t[i]}r=this.element.dataset.imageId?parseInt(this.element.dataset.imageId):0;if(this.carouselImages.length>0){this.setCarouselImage(r);this.carouselElement.style.removeProperty("display")}}};BXUrlPreview.prototype.setCarouselImage=function(e){var t;var i;var r;if(!(e>=0&&e<=this.carouselImages.length-1))return null;this.carouselImages.map(function(e){e.style.display="none"});this.carouselImages[e].style.removeProperty("display");if(i=this.carouselImages[e].querySelector("img")){t=i.getAttribute("src");if(this.inputElement){r=this.inputElement.value.split(";");this.inputElement.value=r[0]+";"+t}}this.currentImageId=e};BXUrlPreview.prototype.nextImage=function(){var e=this.currentImageId+1;if(e>this.carouselImages.length-1)e=0;this.setCarouselImage(e)};BXUrlPreview.prototype.previousImage=function(){var e=this.currentImageId-1;if(e<0)e=this.carouselImages.length-1;this.setCarouselImage(e)};BXUrlPreview.showEmbed=function(){if(BX.hasClass(this,"urlpreview__container-hide-embed")){BX.addClass(this,"urlpreview__container-hide-image");BX.removeClass(this,"urlpreview__container-hide-embed");var e=BX.findChildByClassName(this,"video-js");if(e){if(BX.getClass("BX.Fileman.PlayerManager")){var t=BX.Fileman.PlayerManager.getPlayerById(e.getAttribute("id"));if(t){t.play()}}}else{var i=BX.findChildByClassName(this,"urlpreview-iframe-html-embed");if(i){BXUrlPreview.adjustFrameHeight(i,5)}}}};BXUrlPreview.bindEmbedHandler=function(){var e=document.querySelectorAll(".urlpreview__container-switchable");var t;for(t=0;t<e.length;t++){e.item(t).addEventListener("click",BXUrlPreview.showEmbed)}var i=document.querySelectorAll(".urlpreview:not(.urlpreview__edit) .urlpreview__image img.urlpreview__image-not-inited");for(t=0;t<i.length;t++){i.item(t).addEventListener("load",BXUrlPreview.onEmbeddedImageLoad);i.item(t).classList.remove("urlpreview__image-not-inited")}};BXUrlPreview.onEmbeddedImageLoad=function(e){var t=e.currentTarget;if(BX.type.isDomNode(t)){BX.onCustomEvent(window,"BX.BXUrlPreview.onImageLoaded",[{imageNode:t}])}};BXUrlPreview.adjustFrameHeight=function(e,t){if(BX.hasClass(e,"urlpreview-iframe-html-embed-adjusted")){return}t=t||0;if(t>10){return}var i=50;if(e.contentWindow.document.body.scrollHeight>e.height){e.height=e.contentWindow.document.body.scrollHeight+i+"px";BX.addClass(e,"urlpreview-iframe-html-embed-adjusted");return}var r=e.contentWindow.document.getElementsByTagName("video");if(r[0]){e.height=e.contentWindow.document.body.scrollHeight+i+"px";BX.addClass(e,"urlpreview-iframe-html-embed-adjusted");return}else{var n=e.contentWindow.document.getElementsByTagName("iframe");var l=0;for(var s=0;s<n.length;s++){if(n[s]&&n[s].height>0){l=parseInt(n[s].height)}else if(n[s]&&n[s].style.height){l=parseInt(n[s].style.height)}if(l!==0){e.height=l+i+"px";BX.addClass(e,"urlpreview-iframe-html-embed-adjusted")}}if(l===0){setTimeout(function(){t++;BXUrlPreview.adjustFrameHeight(e,t)},500)}}};
//# sourceMappingURL=script.map.js